home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / arcers / tar316.zip / ZIPPIPE.C < prev    next >
Text File  |  1994-07-21  |  7KB  |  242 lines

  1. #include <stdio.h>
  2. #include "modern.h"
  3. #include "zalloc.h"
  4. #include "zippipe.h"
  5. #include "zipguts.h"
  6. #include "ziposcod.h"
  7. #include "crc32.h"
  8. #ifndef UNIX
  9. #    ifdef M_XENIX
  10. #        define UNIX
  11. #    endif
  12. #    ifdef unix
  13. #        define UNIX
  14. #    endif
  15. #endif
  16. #ifdef UNIX
  17. #    include <time.h>
  18. #endif
  19. #ifdef MSDOS
  20. #    include <dos.h>
  21. #    ifdef __TURBOC__
  22. #        include <io.h>
  23. #    endif
  24. #endif
  25.  
  26. static unsigned long dostime __ARGS__((void))
  27. {
  28. #ifdef MSDOS
  29. # ifdef __TURBOC__
  30.    union { struct date d; struct time t; } u;
  31.    struct ftime f;
  32.  
  33.    getdate(&u.d);
  34.    f.ft_year  = u.d.da_year - 1980;
  35.    f.ft_month = u.d.da_mon;
  36.    f.ft_day   = u.d.da_day;
  37.  
  38.    gettime(&u.t);
  39.    f.ft_hour  = u.t.ti_hour;
  40.    f.ft_min   = u.t.ti_min;
  41.    f.ft_tsec  = u.t.ti_sec;
  42.  
  43.    return *(unsigned long *)&f;
  44. # else
  45.    uninon REGS r;
  46.    unsigned d;
  47.  
  48.    r.h.ah = 0x2A; /* get date */ intdos(&r, &r);
  49.    d = ((r.x.cx - 1980) << 9) | (r.h.dh << 5) | r.h.dl;
  50.  
  51.    r.h.ah = 0x2C; /* get time */ intdos(&r, &r);
  52.    return ((unsigned long)d << 16) |
  53.       (r.h.ch << 11) | (r.h.cl << 5) | (r.h.dh >> 1);
  54. # endif
  55. #else
  56. # ifdef UNIX
  57.    extern long time();
  58.    struct tm *s;
  59.    long t;
  60.  
  61.    (void)time(&t);
  62.    s = gmtime(&t);
  63.    if (s->tm_year < 80) return 0L;
  64.    return ((unsigned long)(s->tm_year - 80) << 25) |
  65.       ((unsigned long)s->tm_mon << 21) | ((unsigned long)s->tm_mday << 16) |
  66.       ((unsigned)s->tm_hour << 11) | (s->tm_min << 5) | (s->tm_sec >> 1);
  67. # else
  68.    /* dummy time stamp */ return 0L;
  69. # endif
  70. #endif
  71. }
  72.  
  73. #ifndef zalloc
  74. /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  75.  * and farmalloc(64K) returns a pointer with nonzero offset, so we
  76.  * must fix the pointer. Warning: the pointer must be saved in its
  77.  * original form in order to free it, use farfree().
  78.  * For MSC, use halloc instead of this function.
  79.  */
  80. void far *zalloc(void far **p, unsigned n, unsigned s)
  81. {
  82.    register unsigned long l;
  83.    l = (unsigned long)(*p = farmalloc((unsigned long)n*s + 15));
  84.    return (void far *)
  85.       ((0xffff0000L & l) + (0xffff0000L & (((0xffffL & l) + 15) << 12)));
  86. }
  87. #endif
  88.  
  89. int zipalloc()
  90. {
  91. #ifdef DYN_ALLOC
  92.    if (ct_alloc() != 0) return ZNOMEM;
  93.    if (lm_alloc() != 0) {
  94.       ct_free(); return ZNOMEM;
  95.    }
  96. #endif
  97.    return 0;
  98. }
  99.  
  100. void zipfree()
  101. {
  102. #ifdef DYN_ALLOC
  103.    lm_free();
  104.    ct_free();
  105. #endif
  106. }
  107.  
  108. static void putlong __ARGS__((ulg));
  109.  
  110. static void putlong(l)
  111. ulg l;
  112. {
  113.    putword((ush)l); putword((ush)(l >> 16));
  114. }
  115.  
  116. static ulg inpsize;
  117. #ifdef DEBUG
  118.        ulg isize;
  119. #endif
  120. static int ziptype;
  121. int deflate_level = 6;
  122. static ulg timestamp;
  123. static ush flags;
  124.  
  125. /* speed options for the general purpose bit flag */
  126. #define FAST 4
  127. #define SLOW 2
  128.  
  129. int zipcreat(putb, ztype, dlevel)
  130. void (*putb)__ARGS__((int));
  131. int ztype, dlevel;
  132. {
  133.    register k;
  134.  
  135.    if (dlevel<1  || dlevel>9 ||
  136.       (ztype!=ZIP_PKW && ztype!=ZIP_GNU && ztype!=ZIP_RAW))
  137.       return (ziperror = ZUNSUP);
  138.    deflate_level = dlevel;
  139.    flags = dlevel <= 1 ? FAST : dlevel >= 9 ? SLOW : 0;
  140.    ziptype = ztype;
  141.  
  142.    crcbegin();
  143.    bi_init();
  144.    if (ct_init() != 0) return (ziperror = ZNOMEM);
  145.    if ((k=lm_init()) != 0) {
  146. #ifdef DYN_ALLOC
  147.       ct_free();
  148. #endif
  149.       return (ziperror = k);
  150.    }
  151.    ziputbyte = putb;
  152.    /* Write the header to the gzip file */
  153.    /* No extra field, file name or comment; no encryption */
  154.    if (ziptype == ZIP_GNU) {
  155.       putword(GZIP_MAGIC);/* magic header */
  156.       putbyte(DEFLATED);  /* compression method */
  157.       putbyte(0);         /* general flags: nothing */
  158.       putlong(0L);        /* dummy time stamp */
  159.       putbyte(flags);     /* extra flags */
  160.       putbyte(OS_CODE);   /* OS identifier */
  161.    } else if (ziptype == ZIP_PKW) {
  162.       putlong(PKW_LOCAL); /* magic header */
  163.       putword(19);        /* version to extract */
  164.       putword(flags|=8);
  165.       putword(DEFLATED);  /* compression method */
  166.       /* Who, the hell, needs in this time stamp? */
  167.       putlong(timestamp = dostime());
  168.       putlong(0L); /* dummy CRC */
  169.       putlong(0L); /* dummy compressed size */
  170.       putlong(0L); /* dummy original size */
  171.       putlong(0L); /* null file name & extra fields */
  172.    }
  173.    inpsize = 0L;
  174.    return 0;
  175. }
  176.  
  177. int zipwrite(buffer, length)
  178. char *buffer; unsigned length;
  179. {
  180.    if (!ziputbyte) return (ziperror=ZNOPEN, -1);
  181.    if (length) {
  182.       updcrc((unsigned char *)buffer, length);
  183.       inpsize += length;
  184.    }
  185.    return deflate_level > 3 ?
  186.             lazy_deflate(buffer, length) :
  187.             fast_deflate(buffer, length);
  188. }
  189.  
  190. long zipclose()
  191. {
  192.    extern unsigned minlookahead;
  193.    register long l = -1;
  194.    ulg clen, crc;
  195.  
  196.    minlookahead = 0; /* indicate end of input */
  197.    /* Flush out any remaining bytes */
  198.    if (zipwrite(NULL,0) != 0) goto end;
  199.    clen = (compressed_len >> 3);
  200.    crc = getcrc();
  201.    if (ziptype == ZIP_GNU) {
  202.       /* Write the crc. & uncompressed size */
  203.       putlong(crc); putlong(inpsize);
  204.    } else if (ziptype == ZIP_PKW) {
  205.       /* Write the /data descriptor/ extended locxal header */
  206.       putlong(PKW_EXT); /* signature */
  207.       putlong(crc);     /* CRC */
  208.       putlong(clen);    /* compressed size */
  209.       putlong(inpsize); /* uncompressed size */
  210.       /* Write the central directory entry */
  211.       putlong(PKW_CENTRAL);
  212.       putword((OS_CODE<<8)|20); /* version made by */
  213.       putword(19);      /* version to extract */
  214.       putword(flags);
  215.       putword(DEFLATED);/* compression method */
  216.       putlong(timestamp);
  217.       putlong(crc);     /* CRC */
  218.       putlong(clen);    /* compressed size */
  219.       putlong(inpsize); /* original size */
  220.       putlong(0L);      /* filename & extra field length */
  221.       putword(0);       /* file comment length */
  222.       putword(0);       /* disk number start */
  223.       putword(0);       /* internal file attributes */
  224.       putlong(0L);      /* external file attributes */
  225.       putlong(0L);      /* relative offset of local header */
  226.       /* Finish the central directory */
  227.       putlong(PKW_END);
  228.       putword(0); /* number of this disk */
  229.       putword(0); /* number of the disk with the start of CD */
  230.       putword(1); /* total number of CD entries on this disk */
  231.       putword(1); /* total number of CD entries */
  232.       putlong(46L); /* size of the central directory */
  233.       putlong(30+clen+16); /* offset of start of CD */
  234.       putword(0); /* zipfile comment length */
  235.    }
  236.    l = clen;
  237. end:
  238.    ziputbyte = NULL;
  239.    zipfree();
  240.    return l;
  241. }
  242.